/*_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
_/                                                                            _/
_/                      _/  _/                  _/                            _/
_/       _/_/_/    _/_/_/  _/_/_/      _/_/_/  _/_/_/      _/_/_/  _/  _/_/   _/
_/    _/    _/  _/    _/  _/    _/  _/    _/  _/    _/  _/    _/  _/_/        _/
_/   _/    _/  _/    _/  _/    _/  _/    _/  _/    _/  _/    _/  _/           _/
_/    _/_/_/    _/_/_/  _/_/_/      _/_/_/  _/_/_/      _/_/_/  _/            _/
_/       _/                                                                   _/
_/   _/_/                              Free software copyrighted since 2007   _/
_/                                              <gdbabar@gmail.com>           _/
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/*/

#include <dirent.h>
#include <locale.h>

#include <gtk/gtk.h>

#include "common.h"

gboolean
is_first_run (void)
{

  gchar *filename;
  gboolean file_exist;

  filename = g_strconcat (g_get_home_dir (), HOME_CONF_DIR"/gdbabar.conf", NULL);

  file_exist = g_file_test (filename, G_FILE_TEST_EXISTS);

  g_free (filename);

  return !file_exist;

}

void
common_infobox (GtkWindow *parent, gchar *text)
{

  GtkWidget *dialog;

  dialog = gtk_message_dialog_new (parent,
                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                   GTK_MESSAGE_INFO,
                                   GTK_BUTTONS_OK,
                                   "%s",
                                   text);

  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);

}

void
destroy_window_signal (GtkObject *object, gpointer user_data)
{

  g_message ("Destroy signal received ... bye bye ;)");
  gtk_main_quit ();

}

void
g_file_copy (gchar *src_file, gchar *dest_file)
{

  GMappedFile *file;
  gchar *contents;

  file = g_mapped_file_new (src_file, FALSE, NULL);
  contents = g_mapped_file_get_contents (file);

  g_file_set_contents (dest_file, contents, g_mapped_file_get_length (file), NULL);

  g_mapped_file_free (file);

}

gchar*
get_launcher_file_from_index (int index)
{

  gchar *file,*key, *filename = NULL;
  GKeyFile *key_file;

  file = g_strconcat (g_get_home_dir (), HOME_CONF_DIR"/desktop/.order", NULL);

  key_file = g_key_file_new ();

  if (g_key_file_load_from_file (key_file, file, G_KEY_FILE_NONE, NULL))
    {
      key = g_strdup_printf ("%u", index);
      filename = g_key_file_get_string (key_file, "Order", key, NULL);

      g_free (key);
    }

  g_key_file_free (key_file);
  g_free (file);

  return filename;

}

gchar*
get_launcher_name_from_index (int index)
{

  gchar *file, *filename, *name = NULL;
  GKeyFile *key_file;

  filename = get_launcher_file_from_index (index);
  file = g_strconcat (g_get_home_dir (), HOME_CONF_DIR"/desktop/", filename, NULL);
  g_free (filename);

  key_file = g_key_file_new ();

  if (g_key_file_load_from_file (key_file, file, G_KEY_FILE_NONE, NULL))
    {
      name = g_key_file_get_string (key_file, "Desktop Entry", "Name", NULL);
    }

  g_key_file_free (key_file);
  g_free (file);

  return name;

}

gchar*
get_launcher_exec_from_index (int index)
{

  gchar *file, *filename, *exec = NULL;
  GKeyFile *key_file;

  filename = get_launcher_file_from_index (index);
  file = g_strconcat (g_get_home_dir (), HOME_CONF_DIR"/desktop/", filename, NULL);
  g_free (filename);

  key_file = g_key_file_new ();

  if (g_key_file_load_from_file (key_file, file, G_KEY_FILE_NONE, NULL))
    {
      exec = g_key_file_get_string (key_file, "Desktop Entry", "Exec", NULL);
    }

  g_key_file_free (key_file);
  g_free (file);

  return exec;

}

gchar*
get_launcher_icon_path_from_index (int index)
{

  gchar *file, *filename, *ico_path = NULL;
  GKeyFile *key_file;

  filename = get_launcher_file_from_index (index);
  file = g_strconcat (g_get_home_dir (), HOME_CONF_DIR"/desktop/", filename, NULL);
  g_free (filename);

  key_file = g_key_file_new ();

  if (g_key_file_load_from_file (key_file, file, G_KEY_FILE_NONE, NULL))
    {
      ico_path = g_key_file_get_string (key_file, "Desktop Entry", "Icon", NULL);
    }

  g_key_file_free (key_file);
  g_free (file);

  return ico_path;

}

void
create_empty_desktop_entry_file (gchar *filename)
{

  g_file_set_contents (filename, "[Desktop Entry]\n"\
                       "Version=1.0\n"
                       "Encoding=UTF-8\n"
                       "Name=\n"
                       "Type=Application\n"
                       "Exec=\n"
                       "Icon=",
                       -1,
                       NULL);

}

guint
get_launcher_number (void)
{

  gchar *path;
  DIR *dir;
  struct dirent *ep;
  guint i = 0;

  path = g_strconcat (g_get_home_dir (), HOME_CONF_DIR"/desktop/", NULL);

  dir = opendir (path);

  if (dir)
    {
      while ((ep = readdir (dir)))
        {
          if (g_str_has_suffix (ep->d_name, ".desktop"))
            {
              i++;
            }
        }
    }

  closedir (dir);
  g_free (path);

  return i;

}

